#include <bits/stdc++.h>
#define N 1004
#define mod 1000000007
using namespace std;
typedef long long ll;

int n, a[N], b[N];
int posx[N], posy[N], f[N][N];
bool tg[N][N];
bool rec=1;

int main ()
{
    freopen("zha.in", "r", stdin);
    freopen("zha.out", "w", stdout);

    scanf("%d", &n);
    for (int i=1; i<=n; i++) { scanf("%d", &a[i]); posx[a[i]]=i; if (a[i]!=i) { rec=0; } }
    for (int i=1; i<=n; i++) { scanf("%d", &b[i]); posy[b[i]]=i; if (b[i]!=i) { rec=0; } }

    for (int i=1; i<=n; i++) {
        tg[posx[i]][posy[i]]=1;
    }

    for (int i=1; i<=n; i++) {
        for (int j=1; j<=n; j++) {
            if (i==1 && j==1) { f[i][j]=1; continue; }
            f[i][j]=(f[i-1][j]+f[i][j-1])%mod;
            if (rec && tg[i][j]) { f[i][j]=f[i-1][j]; }
            else if (tg[i][j]) {
                for (int k=1; k<=n; k++) {
                    if (i>posx[k] && j>posy[k] && i-posx[k]==j-posy[k]) { f[i][j]-=f[posx[k]][posy[k]]; f[i][j]%=mod; }
                }
            }
        }
    }

    f[n][n]=(f[n][n]%mod+mod)%mod;
    printf("%d\n", f[n][n]);

    return 0;
}